#include <SDL/SDL.h>
#include "memory.h"
#include "screen.h"
#include "image.h"
#include "tilemap.h"


Maplayer::Maplayer(long wide, long high) {
    /* 
	if(wide>MAPLAYER_WIDE_MAX) return;
    if(high>MAPLAYER_HIGH_MAX) return;
	*/
    _wide = wide;
    _high = high;
    _tilewide = MAPLAYER_TILE_WIDE;
    _tilehigh = MAPLAYER_TILE_HIGH;
    _tilecount = 0;
	_phase = 0;

}

Maplayer::~Maplayer()  {
}

/*
Terrainlayer Maplayer::terrain() {
  return _terrain;
}

*/

int Maplayer::add(Maptilepointer tile) {
    int result		= _tilecount;
    _tiles[result] 	= tile;
    _tilecount++;
    return result;    
}

Maplayer const * Maplayer::set(int x, int y, int tile_id, int phase) {
    _layer[x][y] = _tiles[tile_id];
    return this;
}

Image Maplayer::get(int x, int y) {
    return _layer[x][y]->image_get(_phase);
}

Maplayer const * Maplayer::draw(Screen screen, int x, int y ) {  
		long now = SDL_GetTicks();
		if((now - _delay) > 500) {
			_phase ++;
			if(_phase > 7) _phase = 0;
			_delay = now;
		}
		// Animate the tile phases
        int txstart = ( x / _tilewide );
        int tystart = ( y / _tilehigh );
        int _xtilestop  = (screen.wide() / _tilewide) + 1 ;
        int _ytilestop  = (screen.high() / _tilehigh) + 1 ;        
        int txstop = _xtilestop + txstart;
        int tystop = _ytilestop + tystart;
        int drawx  = 0;
        int drawy  = 0;
		int txdex  ;
		int tydex  ;
        Maptilepointer *row;
        Image aidimage = NULL;

        if (txstart >= _wide || tystart >= _high) { 
            return this;
        } 
        if (txstart < 0) txstart = 0;
        if (tystart < 0) tystart = 0;
        if (txstop >= _wide) txstop = _wide;
        if (tystop >= _high) tystop = _high;        
        
        drawx = -x + ( (txstart-1) * _tilewide );

        for (txdex = txstart; txdex < txstop; txdex ++ ) {
            drawx   += _tilewide;            
            drawy   = -y + ( (tystart-1) * _tilehigh );
            row     = _layer[txdex];            
            if(row == NULL) continue;
            
            for (tydex = tystart; tydex < tystop; tydex ++ ) {                
                aidimage    = row[tydex]->image_get(_phase);                
                if(aidimage == NULL) continue;
                /* Get the tile. If it is null, immediately skip to the next tile. */
                drawy   += _tilehigh;                
                /* The tile rendered can never be completely off screen. */
                screen.blit(drawx, drawy, aidimage);                
            }
        
        }
        return this;
}


Rope Tilemap::lua_export() {
	Rope result;
	result += "erutamap = {}\n"; 
    result += "erutamap.layer_count = ";
	result += layer_count ;
	result += "\n";
	result += "erutamap.wide = ";
	result +=  wide;
	result +=  "\n";
    result += "erutamap.high = "; 
	result +=  high; 
	result += "\n" ;
	result += "erutamap.layers = {}\n";
	result += "erutamap.background = ""\n";
	result += "erutamap.entities   = {}\n";
	return result;
}


